import { keepFileApi } from "@/api/client/documentManagement/waybillManagement";
import FormHook from "@/hook/formHook";
import { GeneraDataType } from "@/interface";
import { message } from "ant-design-vue";
export default function OperationHook(state: GeneraDataType) {
  const { formState, formDataRef, emit, passData, loading } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const onFinish = () => {
    const { file, objectType } = formState;
    const params = {
      ...file[0],
      objectType,
      businessId: passData.value.id
    };
    loading.value = true;
    keepFileApi(params)
      .then((res: any) => {
        const { code, message: Message } = res;

        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
        if (code === 500 && Message === "Maximum upload size exceeded") {
          return Message.success("上传文件不允许超过50MB!");
        }
      })
      .finally(() => (loading.value = false));
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields,
    validateZhInputHandle
  };
}
